﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "GamePlay/PaperPlayerController.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
#include "PaperZDCharacter.h"
#include "GameFramework/CharacterMovementComponent.h"


void APaperPlayerController::BeginPlay()
{
	Super::BeginPlay();
	//绑定增强输入系统
	check(AuraContext);
	//多人游戏中，不是本地玩家无法通过GetLocalPlayer获取到对应自己的输入组件所以需要做空指针判断
	UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
	if (Subsystem)
	{
		Subsystem->AddMappingContext(AuraContext, 0);
	}
	PaperZDCharacter= Cast<APaperZDCharacter>(GetPawn());
}

void APaperPlayerController::SetupInputComponent()
{
	Super::SetupInputComponent();
	UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent);
	if (EnhancedInputComponent)
	{
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &APaperPlayerController::Move);
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &APaperPlayerController::SetMoveInputValue,0.f);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &APaperPlayerController::Jump);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &APaperPlayerController::StopJump);
	}
}

void APaperPlayerController::Move(const FInputActionValue& InputActionValue)
{
	//使用红色字体打印消息到屏幕上
	const FVector2D InputAxisVector = InputActionValue.Get<FVector2D>();
	if (PaperZDCharacter)
	{
		PaperZDCharacter->AddMovementInput(FVector::ForwardVector, InputAxisVector.X);
		SetMoveInputValue(InputAxisVector.X);
	}
	
}

void APaperPlayerController::Jump()
{
	if (PaperZDCharacter&&IsGround())
	{
		PaperZDCharacter->Jump();
	}
}

void APaperPlayerController::StopJump()
{
	if (PaperZDCharacter)
	{
		PaperZDCharacter->StopJumping();
	}
}

bool APaperPlayerController::IsGround() const
{
	if (PaperZDCharacter)
	{
		return PaperZDCharacter->GetCharacterMovement()->IsMovingOnGround();
	}
	return false;
}

bool APaperPlayerController::IsLanding() const
{
	if (PaperZDCharacter)
	{
		return IsGround()&&MoveInputValue==0.f;
	}
	return false;
}

void APaperPlayerController::SetMoveInputValue(const float InuptValue)
{
	MoveInputValue = InuptValue;
}

